package learn.sort;

/**
 * @author 于昂
 * Date 2021/1/9
 * Time 14:37
 */
public class SelectSort {
    public static void main(String[] args) {
//        int[] array = {3, 9, -1, -10, -2};
        int[] array = new int[80000];
        for (int i = 0; i < array.length; i++) {
            array[i] = (int) (Math.random() * 800000);
        }
        Long startTime = System.currentTimeMillis();
        sort(array);
        Long endTime = System.currentTimeMillis();
        long tempTime = (endTime - startTime);
        System.out.println("花费时间：" +
                (((tempTime / 86400000) > 0) ? ((tempTime / 86400000) + "d") : "") +
                ((((tempTime / 86400000) > 0) || ((tempTime % 86400000 / 3600000) > 0)) ? ((tempTime % 86400000 / 3600000) + "h") : ("")) +
                ((((tempTime / 3600000) > 0) || ((tempTime % 3600000 / 60000) > 0)) ? ((tempTime % 3600000 / 60000) + "m") : ("")) +
                ((((tempTime / 60000) > 0) || ((tempTime % 60000 / 1000) > 0)) ? ((tempTime % 60000 / 1000) + "s") : ("")) +
                ((tempTime % 1000) + "ms"));
//        for (int i : array) {
//            System.out.print(i + " ");
//        }
    }

    private static void sort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            exchangeMin(i, array);
        }
    }

    /**
     * @param i     数组遍历的起点和待交换的元素index
     * @param array 待排序的数组
     * @Method 找到数组的最小值，并且让最小值的位置与第i个元素位置交换
     * @Return void
     * @Date 2021-01-09 14:44
     */
    public static void exchangeMin(int i, int[] array) {
        int minIndex = i;
        int min = array[i];
        boolean flag = false;
        for (int j = i; j < array.length - 1; j++) {
             if (array[j + 1] < min) {
                 flag = true;
                min = array[j + 1];
                minIndex = j + 1;
            }
        }
        if (flag) {
            int temp = array[i];
            array[i] = min;
            array[minIndex] = temp;
            flag = false;
        }
    }
}
